// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        rainPrefab: {
            default: null,
            type: cc.Prefab
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // 生成一个红包雨
        this.spawnNewRain();
    },

    start () {

    },

    // update (dt) {},
    // 生成一个红包雨
    spawnNewRain() {
        // 创建一个红包雨对象
        var newRain = cc.instantiate(this.rainPrefab);
        // 将新增的红包雨对象添加到canvas中
        this.node.addChild(newRain);
        // 为红包雨设置一个随机位置
        var randX = 0;
        var maxX = this.node.width/2;
        randX = (Math.random() - 0.5) * 2 * maxX;
        newRain.x = randX;
        newRain.y = this.node.height/2;
    }
});
